<?php
namespace Home\Controller;
use Think\Controller;
class MessageController extends Controller
{
    public function index()
    {
        $this->XS();
        $id=I("id");
        session('id',$id);

        if(session('username')){
            $this->assign('username',session('username'));
            $news = M('article');
            $news_list = $news->where("id=".$id)->select();
            $news_list = $this->filter($news_list);
            $this->assign('news',$news_list);
            $this->display();

        }else{
            //当用户没有登录时，跳转到登录页面
            $this->error("请先登录",U('/Home/Index/login/'));
        }

    }
    public function XS()
    {
        $id=I('id');
//        var_dump($id);
//        exit();
        if(session('username')){
            $this->assign('username',session('username'));
            $news = M('pl');
            $news_list = $news->where("pid=".$id)->select();
            echo $news->getLastSql();

            $news_list = $this->filter($news_list);
            $this->assign('new',$news_list);
            $this->display();
//            $this->success("评论成功",U('/Home/Message/index/'));
        }else{
            //当用户没有登录时，跳转到登录页面
            $this->error("请先登录",U('/Home/Index/login/'));
        }
    }
    public function mess(){
        session('id');
//        var_dump(session('id'));
//        exit();
        $User = M("pl");

    $data=array(
        "pid"=> session('id'),
        "author"=> session('username'),
        "message"=>I("content"),
        "createtime"=>time()
    );
//    var_dump($data);
//    exit();
            if($User->add($data)){
//                    var_dump($data);
//    exit();
                redirect(U("/Home/News/index/"));
            }else{
                echo $User->getlastSql();
                        exit();
                $this->error('评论失败');
            }
            $this->error($User->getError());


    }

    public function  filter($param){

        foreach ($param as $key => $value){
            $param[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
            if(mb_strlen($value['author'],'utf-8') >8){
                $param[$key]['author'] = mb_substr($value['author'],0,8,'utf-8')."......";
            }

        }
        return $param;
    }
    public function  first(){

        redirect(U("/Home/News/index/"));

    }
    public function  juns(){
        $cid=I("id");
        redirect(U("/Home/Jun/index/",array('cid'=>$cid)));

    }
    public function  yul(){
        $cid=I("id");
        redirect(U("/Home/Yl/index/",array('cid'=>$cid)));

    }
    public function  china(){
        $cid=I("id");
        redirect(U("/Home/Ch/index/",array('cid'=>$cid)));

    }
    public function quit(){
        if(session('username')){
            session(null);
        }

        redirect(U('/Home/Index/login/'),0,"重新登录");
    }
}